package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.server.service.PreBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/prebook")
@Slf4j
public class PreBookController {
    
    @Autowired
    private PreBookService preBookService;
    
    /**
     * 处理预定图书逻辑
     */
    @PostMapping("/process/{bookId}")
    public Result processPreBook(@PathVariable int bookId) {
        return preBookService.processPreBook(bookId);
    }
    
    /**
     * 处理超时预定（定时任务调用或手动触发）
     */
    @PostMapping("/handleOverdue")
    public Result handleOverduePreBooks() {
        return preBookService.handleOverduePreBooks();
    }
    
    /**
     * 完成预定借阅
     */
    @PostMapping("/complete")
    public Result completePreBookBorrow(@RequestParam int preId, @RequestParam int managerId) {
        return preBookService.completePreBookBorrow(preId, managerId);
    }
    
    /**
     * 获取未借阅的预定记录（is_borrowed=0且inform_time不为空）
     */
    @GetMapping("/unborrowed")
    public Result getUnborrowedPreBooks(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        return preBookService.getUnborrowedPreBooks(page, pageSize);
    }
    
    /**
     * 获取已借阅的预定记录（is_borrowed=1且inform_time不为空）
     */
    @GetMapping("/borrowed")
    public Result getBorrowedPreBooks(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return preBookService.getBorrowedPreBooks(page, pageSize);
    }
    
    /**
     * 根据读者编号或姓名模糊查询相关预定记录
     */
    @GetMapping("/search")
    public Result getPreBooksByReaderKeyword(@RequestParam String keyword,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        return preBookService.getPreBooksByReaderKeyword(keyword, page, pageSize);
    }
}